package com.tengju.bff.interfaces.manage.crm.dashborad.facade;

import com.tengju.bff.interfaces.app.domination.vo.StarLevelNumVO;
import com.tengju.bff.interfaces.manage.crm.user.assembler.CrmDominationAssembler;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.data.domain.model.clickhouse.star.DominationMemberNew;
import com.tengju.data.domain.model.clickhouse.star.DominationSigningMember;
import com.tengju.data.domain.model.clickhouse.star.DominationUpComing;
import com.tengju.data.domain.model.user.*;
import com.tengju.data.domain.shared.DateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class DashboardDominationTeamFacade {

    private final DominationDetailRepository dominationDetailRepository;

    public List<StarLevelNumVO> memberNewNum(List<UserIdCode> idCodes, DominationLevelEnum dominationLevel) {
        if (CollectionUtils.isEmpty(idCodes)) {
            return zero();
        }
        //本月时间区间
        LocalDateTime timeFrom = DateUtil.getFirstDayOfMonthMin();
        LocalDateTime timeTo = LocalDateTime.now();
        //count
        DominationMemberNew dominationMemberNew =
                dominationDetailRepository
                        .countNewMemberCurrentMonthByIdCodes(idCodes, dominationLevel, timeFrom, timeTo);
        return CrmDominationAssembler.assemblerToVO(dominationMemberNew);
    }


    public List<StarLevelNumVO> signingMember(List<UserIdCode> idCodes, DominationLevelEnum dominationLevel) {
        if (CollectionUtils.isEmpty(idCodes)) {
            return zero();
        }
        //count
        DominationSigningMember dominationSigningMember = dominationDetailRepository
                .countSigningMemberByIdCodesDominationLevel(idCodes, dominationLevel);
        return CrmDominationAssembler.assemblerToVO(dominationSigningMember);
    }


    public List<StarLevelNumVO> upComing(List<UserIdCode> idCodes, DominationLevelEnum dominationLevel) {
        if (CollectionUtils.isEmpty(idCodes)) {
            return zero();
        }
        //count
        DominationUpComing dominationUpComing = dominationDetailRepository
                .countUpComingMemberByIdCodes(idCodes, dominationLevel);
        return CrmDominationAssembler.assemblerToVO(dominationUpComing);
    }


    private List<StarLevelNumVO> zero() {
        return List.of(StarLevelNumVO.of(StarLevelEnum.CITY_STAR_ONE, 0),
                StarLevelNumVO.of(StarLevelEnum.CITY_STAR_TWO, 0),
                StarLevelNumVO.of(StarLevelEnum.CITY_STAR_THREE, 0),
                StarLevelNumVO.of(StarLevelEnum.CITY_STAR_FOUR, 0),
                StarLevelNumVO.of(StarLevelEnum.EC, 0),
                StarLevelNumVO.of(StarLevelEnum.SEC, 0),
                StarLevelNumVO.of(StarLevelEnum.EEC, 0));
    }
}